package com.wande.dataplatform.mapper;

import com.wande.common.mybatis.core.mapper.BaseMapperPlus;
import com.wande.dataplatform.domain.ApiCallLog;
import com.wande.dataplatform.domain.vo.ApiCallLogVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * API调用日志 Mapper接口
 *
 * @author wande
 */
public interface ApiCallLogMapper extends BaseMapperPlus<ApiCallLog, ApiCallLogVO> {

    /**
     * 统计API调用次数
     */
    @Select("SELECT COUNT(*) FROM dp_api_call_log WHERE api_id = #{apiId} " +
            "AND call_time BETWEEN #{startTime} AND #{endTime}")
    Long countCalls(@Param("apiId") Long apiId,
                    @Param("startTime") LocalDateTime startTime,
                    @Param("endTime") LocalDateTime endTime);

    /**
     * 统计成功调用次数
     */
    @Select("SELECT COUNT(*) FROM dp_api_call_log WHERE api_id = #{apiId} " +
            "AND status_code = 200 AND call_time BETWEEN #{startTime} AND #{endTime}")
    Long countSuccessCalls(@Param("apiId") Long apiId,
                           @Param("startTime") LocalDateTime startTime,
                           @Param("endTime") LocalDateTime endTime);

    /**
     * 统计平均响应时间
     */
    @Select("SELECT AVG(response_time) FROM dp_api_call_log WHERE api_id = #{apiId} " +
            "AND call_time BETWEEN #{startTime} AND #{endTime}")
    Long avgResponseTime(@Param("apiId") Long apiId,
                         @Param("startTime") LocalDateTime startTime,
                         @Param("endTime") LocalDateTime endTime);

    /**
     * 统计最大响应时间
     */
    @Select("SELECT MAX(response_time) FROM dp_api_call_log WHERE api_id = #{apiId} " +
            "AND call_time BETWEEN #{startTime} AND #{endTime}")
    Long maxResponseTime(@Param("apiId") Long apiId,
                         @Param("startTime") LocalDateTime startTime,
                         @Param("endTime") LocalDateTime endTime);

    /**
     * 统计最小响应时间
     */
    @Select("SELECT MIN(response_time) FROM dp_api_call_log WHERE api_id = #{apiId} " +
            "AND call_time BETWEEN #{startTime} AND #{endTime}")
    Long minResponseTime(@Param("apiId") Long apiId,
                         @Param("startTime") LocalDateTime startTime,
                         @Param("endTime") LocalDateTime endTime);
}
